<?php

namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;

class Stat
{
    /**
     * 平台用户
     */
    public function user()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $mData = [];
            $data = dates();
            foreach ($data as $val) {
                $time = strtotime($val);
                $mData[] = Db::name('user')->where([['register_time','<',$time]])->count();
            }
            $return = [
                'mData' => $mData,
                'data' => $data,
            ];
            return success('',$return);
        } else {
            //用户数量
            $userNum = Db::name('user')->count();
            //一月新增数量
            $firstDayOfMonth = strtotime(date('Y-m-01'));
            $lastDayOfMonth = strtotime(date('Y-m-t'));
            $userMonthNum = Db::name('user')->where([['register_time','between',[$firstDayOfMonth,$lastDayOfMonth]]])->count();
            View::assign('userNum',$userNum);
            View::assign('userMonthNum',$userMonthNum);
            return view();
        }
    }

    /**
     * 购酒量统计
     */
    public function wine()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = [];
            $where[] = ["delete_time","=",0];
            $where[] = ["status",">",0];
            if (!empty($param['nickname'])) {
                $uids = Db::name('user')->where([['nickname','like',"%{$param['nickname']}%"]])->column('id');
                $where[] = ['uid','in',$uids];
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $start_time = strtotime($param['start_time']);
                $end_time = strtotime($param['end_time']);
                $where[] = ['create_time','between',[$start_time,$end_time]];
            }
            $rows = empty($param['limit']) ? get_config('app . page_size') : $param['limit'];
            $order = empty($param['order']) ? 'id desc' : $param['order'];
            $list = Db::name('order')->where($where)->order($order)->paginate($rows, false, ['query' => $param]);
            $list = $list->toArray();
            foreach($list['data'] as &$v)
            {
                $v['create_time'] = datetime($v['create_time']);
                $device = Db::name('device')->where('id',$v['device_id'])->find();
                $user = Db::name('user')->where('id',$v['uid'])->find();
                $v['user_info'] = $user['nickname'].'(ID：'.$v['uid'].')';
                $v['imei'] = $device['imei'];
                $address = json_decode($v['address'],true);
                $v['addr'] = $address['user_name'].'/'.$address['address'];
            }
            $conf['wine_num'] = Db::name('order')->where($where)->sum('wine_num');
            return table_assign(0, $conf, $list);
        }
        else{
            $conf['wine_num'] = Db::name('order')->where([["delete_time","=",0],["status",">",0]])->sum('wine_num');
            View::assign('conf',$conf);
            return view();
        }
    }

    /**
     * 积分兑换酒量统计
     */
    public function score()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = [];
            $where[] = ["pay_type","=",2];
            $where[] = ["delete_time","=",0];
            $where[] = ["status",">",0];
            if (!empty($param['nickname'])) {
                $uids = Db::name('user')->where([['nickname','like',"%{$param['nickname']}%"]])->column('id');
                $where[] = ['uid','in',$uids];
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $start_time = strtotime($param['start_time']);
                $end_time = strtotime($param['end_time']);
                $where[] = ['create_time','between',[$start_time,$end_time]];
            }
            $rows = empty($param['limit']) ? get_config('app . page_size') : $param['limit'];
            $order = empty($param['order']) ? 'id desc' : $param['order'];
            $list = Db::name('order')->where($where)->order($order)->paginate($rows, false, ['query' => $param]);
            $list = $list->toArray();
            foreach($list['data'] as &$v)
            {
                $v['create_time'] = datetime($v['create_time']);
                $device = Db::name('device')->where('id',$v['device_id'])->find();
                $user = Db::name('user')->where('id',$v['uid'])->find();
                $v['user_info'] = $user['nickname'].'(ID：'.$v['uid'].')';
                $v['imei'] = $device['imei'];
                $address = json_decode($v['address'],true);
                $v['addr'] = $address['user_name'].'/'.$address['address'];
            }
            $conf['wine_num'] = Db::name('order')->where($where)->sum('wine_num');
            return table_assign(0, $conf, $list);
        }
        else{
            $conf['wine_num'] = Db::name('order')
                ->where([["delete_time","=",0],["status",">",0],["pay_type","=",2]])->sum('wine_num');
            View::assign('conf',$conf);
            return view();
        }
    }

    /**
     * 售酒机售酒量统计
     */
    public function measure()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = [];
            $where[] = ["delete_time","=",0];
            if (!empty($param['nickname'])) {
                $uids = Db::name('device')->where([['imei','like',"%{$param['nickname']}%"]])->column('id');
                $where[] = ['device_id','in',$uids];
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $start_time = strtotime($param['start_time']);
                $end_time = strtotime($param['end_time']);
                $where[] = ['create_time','between',[$start_time,$end_time]];
            }
            $rows = empty($param['limit']) ? get_config('app . page_size') : $param['limit'];
            $order = empty($param['order']) ? 'id desc' : $param['order'];
            $list = Db::name('order')->where($where)->order($order)->paginate($rows, false, ['query' => $param]);
            $list = $list->toArray();
            foreach($list['data'] as &$v)
            {
                $v['create_time'] = datetime($v['create_time']);
                $device = Db::name('device')->where('id',$v['device_id'])->find();
                $user = Db::name('user')->where('id',$v['uid'])->find();
                $v['user_info'] = $user['nickname'].'(ID：'.$v['uid'].')';
                $v['imei'] = $device['imei'];
                $address = json_decode($v['address'],true);
                $v['addr'] = $address['user_name'].'/'.$address['address'];
            }
            $conf['wine_num'] = Db::name('order')->where($where)->sum('wine_num');
            return table_assign(0, $conf, $list);
        }
        else{
            $conf['wine_num'] = Db::name('order')
                ->where([["delete_time","=",0],["status",">",0]])->sum('wine_num');
            View::assign('conf',$conf);
            return view();
        }
    }

    /**
     * 补酒统计
     */
    public function bu()
    {
        return view();
    }

    /**
     * 平台收益统计
     */
    public function income()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = [];
            $where[] = ["pay_type","=",1];
            $where[] = ["delete_time","=",0];
            $where[] = ["status",">",0];
            if (!empty($param['nickname'])) {
                $uids = Db::name('user')->where([['nickname','like',"%{$param['nickname']}%"]])->column('id');
                $where[] = ['uid','in',$uids];
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $start_time = strtotime($param['start_time']);
                $end_time = strtotime($param['end_time']);
                $where[] = ['create_time','between',[$start_time,$end_time]];
            }
            $rows = empty($param['limit']) ? get_config('app . page_size') : $param['limit'];
            $order = empty($param['order']) ? 'id desc' : $param['order'];
            $list = Db::name('order')->where($where)->order($order)->paginate($rows, false, ['query' => $param]);
            $list = $list->toArray();
            $total = 0;
            foreach($list['data'] as &$v)
            {
                $v['create_time'] = datetime($v['create_time']);
                $device = Db::name('device')->where('id',$v['device_id'])->find();
                $user = Db::name('user')->where('id',$v['uid'])->find();
                $v['user_info'] = $user['nickname'].'(ID：'.$v['uid'].')';
                $v['imei'] = $device['imei'];
                $address = json_decode($v['address'],true);
                $v['addr'] = $address['user_name'].'/'.$address['address'];
                $v['pay_type_title'] = '微信支付';
                //平台收益
                $money = Db::name('wallet_log')->where(['order_id'=>$v['id']])->sum('num');
                $v['pt_amount'] = $v['amount']-$money;
                $total += $v['pt_amount'];
            }
            $conf['wine_num'] = $total;
            return table_assign(0, $conf, $list);
        }
        else{
            $conf['wine_num'] = 0;
            View::assign('conf',$conf);
            return view();
        }
    }
}